package com.txsj.scan.activity.allocationapply;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.txsj.scan.R;
import com.txsj.scan.activity.allocationapply.fragment.AllAllocationApplyListFragment;
import com.txsj.scan.activity.allocationapply.fragment.AllocationApplyStateFragment;
import com.txsj.scan.adapter.TabFrgPagerAdapter;
import com.txsj.scan.base.BaseActivity;
import com.txsj.scan.base.BasePresenter;
import com.txsj.scan.bean.EventAllocationBean;
import com.txsj.scan.widget.TitleBarView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

public class AllocationApplyActivity extends BaseActivity<BasePresenter> {
    @Override
    protected BasePresenter providePresenter() {
        return null;
    }

    ArrayList<Fragment> fragmentList = new ArrayList<>();
    AllAllocationApplyListFragment allocationApplyListFragment;
    AllocationApplyStateFragment allocationApplyStateFragment;
    private FragmentManager fragmentManager;
    private ViewPager viewPager;
    private RadioGroup radioGroup;
    private RadioButton radio_state;
    int current = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_allocation_apply);
        initView();
    }

    private void initView() {
        EventBus.getDefault().register(this);
        mTitle = (TitleBarView) findViewById(R.id.mTitle);
        mTitle.setRightOnclickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(AllocationApplyActivity.this, AllocationScreenActivity.class);
                startActivityForResult(intent, 101);
            }
        });
        radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radio_state = (RadioButton) findViewById(R.id.radio_state);
        radioGroup.check(R.id.radio_state);
        viewPager = findViewById(R.id.viewPager);
        fragmentManager = getSupportFragmentManager();
        allocationApplyListFragment = new AllAllocationApplyListFragment();
        allocationApplyStateFragment = new AllocationApplyStateFragment();
        fragmentList.add(allocationApplyStateFragment);
        fragmentList.add(allocationApplyListFragment);

        viewPager.setAdapter(new TabFrgPagerAdapter(fragmentManager, fragmentList));
        viewPager.setOffscreenPageLimit(2);
        //设置默认打开第一页
        viewPager.setCurrentItem(0);


        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.radio_state:
                        if (viewPager.getCurrentItem() != 0) {
                            viewPager.setCurrentItem(0);
                            current = 0;
                        }
                        break;
                    case R.id.radio_all:
                        if (viewPager.getCurrentItem() != 1) {
                            viewPager.setCurrentItem(1);
                            current = 1;
                        }
                        break;
                }
            }
        });
    }


    //    状态（-1待审核 -2待出库 -3待入库 -4驳回 -5已完成 -6已作废）
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void refreshStates(EventAllocationBean message) {
        if (current != 0) {
            current = 0;
            viewPager.setCurrentItem(0);
            radioGroup.check(R.id.radio_state);
        }
        try {
            JSONObject json = new JSONObject(message.getScreenJson());
            //startDate, endDate, wareHouseId, employeeId, stateName;
            if (json.has("stateName") && !json.isNull("stateName")) {
                radio_state.setText(json.getString("stateName"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
